<!DOCTYPE html>
<!--suppress JSUnresolvedVariable -->
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HTML5定位</title>
    <script type="text/javascript" src="../../Assets/plugins/baidu/api.js"></script>
    <style type="text/css">
        * {
            margin: 0px;
            padding: 0px;
        }

        body {
            height: 100%;
            overflow: hidden;
        }

        #allmap {
            width: 100%;
            height: 100%;
            position: absolute;
        }
    </style>
</head>
<body>
<div id="allmap"></div>
<script type="text/javascript">
    !function(){
        var Util = {};
        Util.mapIt = function(position){
            var lon = position.coords.longitude;
            var lat = position.coords.latitude;
            var map = new BMap.Map("allmap");
            var point = new BMap.Point("" + lon + "", "" + lat + "");
            map.centerAndZoom(point, 19);
            var gc = new BMap.Geocoder();

            translateCallback = function (point) {
                var marker = new BMap.Marker(point);
                map.addOverlay(marker);
                map.setCenter(point);
                gc.getLocation(point, function (rs) {
                    var addComp = rs.addressComponents;
                    if (addComp.province !== addComp.city) {
                        var sContent =
                            "<div><h4 style='margin:0 0 5px 0;padding:0.2em 0'>你当前的位置是：</h4>" +
                            "<p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>" + addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber + "</p>" +
                            "</div>";
                    }
                    else {
                        var sContent =
                            "<div><h4 style='margin:0 0 5px 0;padding:0.2em 0'>你当前的位置是：</h4>" +
                            "<p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>" + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber + "</p>" +
                            "</div>";
                    }
                    var infoWindow = new BMap.InfoWindow(sContent);
                    map.openInfoWindow(infoWindow, point);
                });
            }
            BMap.Convertor.translate(point, 0, translateCallback);
        };
        Util.locationError = function(error){
            switch (error.code) {
                case error.PERMISSION_DENIED:
                    console.log("User denied the request for Geolocation.");
                    break;
                case error.POSITION_UNAVAILABLE:
                    console.log("Location information is unavailable.");
                    break;
                case error.TIMEOUT:
                    console.log("The request to get user location timed out.");
                    break;
                case error.UNKNOWN_ERROR:
                    console.log("An unknown error occurred.");
                    break;
            }
        };
        // 单次位置请求执行的函数
        Util.getLocation = function(){
            navigator.geolocation.getCurrentPosition(Util.mapIt, Util.locationError);
        };

        // 页面加载时执行getLocation函数
        window.onload = Util.getLocation;
    }();
</script>
</body>
</html>